package com.ptswitch.material.web.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;

import com.ptswitch.material.model.Bom;
import com.ptswitch.material.model.BomMaterial;
import com.ptswitch.material.util.Constant;

@Controller("bomAction")
public class BomAction extends ActionSupport{

	private String like;
	private Bom input;
	private List<BomMaterial> goodBM = new ArrayList<BomMaterial>();
	private List<BomMaterial> emptyCodeBM = new ArrayList<BomMaterial>();
	private List<BomMaterial> nullMaterialBM = new ArrayList<BomMaterial>();
	private List<BomMaterial> notPublishBM = new ArrayList<BomMaterial>();
	private List<BomMaterial> notSuggestBM = new ArrayList<BomMaterial>();
	private List<BomMaterial> stopUseBM = new ArrayList<BomMaterial>();
	private List<BomMaterial> disagreeBM = new ArrayList<BomMaterial>();

	// display
	public String display() {
		Bom bom = bomSer.get(id);
		if (bom == null) {
			throw new RuntimeException("单板表不存在");
		}
		goodBM.clear();
		emptyCodeBM.clear();
		nullMaterialBM.clear();
		notPublishBM.clear();
		notSuggestBM.clear();
		stopUseBM.clear();
		disagreeBM.clear();
		for (BomMaterial bm : bom.getBomMaterials()) {
			if (bm.getCode().isEmpty()) {
				emptyCodeBM.add(bm);
			} else if (bm.getMaterial() == null) {
				nullMaterialBM.add(bm);
			} else if (bm.getMaterial().getState().contains("未发布")) {
				notPublishBM.add(bm);
			} else if (bm.getMaterial().getState().contains("不推荐")) {
				notSuggestBM.add(bm);
			} else if (bm.getMaterial().getState().contains("停用")) {
				stopUseBM.add(bm);
			} else if (!bm.getMaterial().getPcbDecal().equals(bm.getFootprint())) {
				disagreeBM.add(bm);
			} else {
				goodBM.add(bm);
			}
		}
		display.put("bom", bom);
		display.put("emptyCodeBM", emptyCodeBM);
		display.put("nullMaterialBM", nullMaterialBM);
		display.put("notPublishBM", notPublishBM);
		display.put("notSuggestBM", notSuggestBM);
		display.put("stopUseBM", stopUseBM);
		display.put("disagreeBM", disagreeBM);
		display.put("goodBM", goodBM);
		putMsg("本单板含物料：" + bom.getBomMaterials().size());
		return "display";
	}

	public String all() {
		List boms = bomSer.getAll(page.getCurrentRow(), page.getPageSize());
		page.setRowCount(bomSer.countAll());
		display.put("boms", boms);
		putMsg("单板总数：" + page.getRowCount());
		return "list";
	}

	// delete
	public String delete() {
		mustAdmins();
		Bom bom = (Bom) display.get("bom");
		bomSer.delete(bom);
		FileUtils.deleteQuietly(new File(ActionSupport.getSavePath() + bom.getFileName()));
		hlogSer.log(visitor(), Constant.deleteBom, bom);
		return "rList";
	}

	public String deleteAll() {
		mustAdmins();
		for (Bom bom : bomSer.getAll(true)) {
			bomSer.delete(bom);
			FileUtils.deleteQuietly(new File(ActionSupport.getSavePath() + bom.getFileName()));
		}
		hlogSer.log(visitor(), Constant.deleteAllBom);
		return "rList";
	}

	// other
	public String like() {
		List boms = bomSer.getLike(like, page.getCurrentRow(), page.getPageSize());
		page.setRowCount(bomSer.countLike(like));
		display.put("boms", boms);
		return "list";
	}

	// get and set
	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public Bom getInput() {
		return input;
	}

}
